package com.sneakxy.mybase4j.platform.provider;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.sneakxy.mybase4j.base.provider.BaseProvider;
import com.sneakxy.mybase4j.platform.beans.Menu;
import com.sneakxy.mybase4j.platform.repository.MenuRepository;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Service(version = "1.0.0", interfaceClass = IMenuProvider.class)
@org.springframework.stereotype.Service
public class MenuProvider extends BaseProvider<Menu, String, MenuRepository> implements IMenuProvider {

	@Autowired
	public MenuProvider(MenuRepository repository) {
		super(repository);
	}

	/* 
	 * 一次性读取可用目录删除空目录，级联多次查询比较浪费资源
	 * @see com.sneakxy.mybase4j.platform.provider.IMenuProvider#findByAccountId(java.lang.String)
	 */
	@Override
	public List<Menu> findByAccountId(String account_id, String type) {
		List<Menu> result = this.getRepository().findByAccountId(account_id, type);
		Iterator<Menu> it = result.iterator();
		Menu menu = null;
		while(it.hasNext()) {
			menu = it.next();
			menu.setChildren(this.findByAccountId(account_id, menu.getId()));
			if(menu.getChildren().isEmpty() && StringUtils.isBlank(menu.getRouter())) {
				it.remove();
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.platform.provider.IMenuProvider#findTreeByType(java.lang.String)
	 */
	@Override
	public List<Menu> findTreeByType(String type) {
		List<Menu> result = this.getRepository().findByType(type);
		Iterator<Menu> it = result.iterator();
		Menu menu = null;
		while(it.hasNext()) {
			menu = it.next();
			menu.setChildren(this.findTreeByType(menu.getId()));
		}
		return result;
	}

}
